package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author Azer
 * @LOVE,,,
 * @Date 2021/11/17 17:12
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //1.参数检测
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //分页参数检查
        dto.checkParam();
        //按名称模糊分页查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNoneBlank(dto.getName())) {

            lambdaQueryWrapper.like(AdChannel::getName, dto.getName());
        }
        //添加排序
        lambdaQueryWrapper.orderByAsc(AdChannel::getOrd);
        IPage result = page(page, lambdaQueryWrapper);
        //3.结果封装
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    /**
     * 新增频道
     * @param channel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel channel) {
        //1.参数校验
        if(channel==null || StringUtils.isBlank(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.判断该频道是否存在
        int count=this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,channel.getName()));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }

        //3.执行新增
        channel.setCreatedTime(new Date());
        save(channel);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     * 修改频道及频道状态
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1 参数校验
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        AdChannel channel = getById(adChannel.getId());
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");

        }
        //3.校验名称唯一性
        if(StringUtils.isBlank(adChannel.getName()
        ) && !adChannel.getName().equals(channel.getName())

        ){
            int count=this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,adChannel.getName()));

            if(count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");

            }
        }
        updateById(adChannel);
        //4.返回结果
        return ResponseResult.okResult();
    }

    /**
     * 频道删除
     * @param id
     * @return
     */
    @Transactional
    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        // 启用状态下不能删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道有效无法操作");
        }
//        int i=1/0;
        //3.删除频道
        removeById(id);
        if(id==46){

            //return不会触发事务
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"id为7不允许被删除");

            throw new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW,"id为46不允许被删除");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
